package com.dao.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.common.base.BaseSqlMapDAO;
import com.model.auth.User;


public class UserDao  extends BaseSqlMapDAO{
	
	
	/**
	 * 根据用户名查询用户
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	public User getUserByName(String userName) throws Exception{
		
		return (User)this.getSqlMapClientTemplate().queryForObject("muser.getUserByName",userName);
	}
	
	
	
	/**
	 * 根据用户ID 查询出所有的用户资源
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<String> getUserRolesResId(Integer userId) throws Exception{
		
		return this.getSqlMapClientTemplate().queryForList("muser.getUserRolesResId",userId);
	}
	
	
	/**
	 * 分页查询所有用户
	 * @param offset 偏移量
	 * @param max 条数
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<User> getUserPageList(int pageNo,int max) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("offset", (pageNo-1)*max<0?0:(pageNo-1)*max);
		map.put("max", max);
		return this.getSqlMapClientTemplate().queryForList("muser.getUserPageList",map);
	}
	
	/**
	 * 查询用户总数
	 * @return
	 * @throws Exception
	 */
	public int getUserCount() throws Exception{
		
		return Integer.valueOf(this.getSqlMapClientTemplate().queryForObject("muser.getUserCount").toString());
	}
	
	
	/**
	 * 插入用户
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public Integer insertUser(User user) throws Exception{
		
		Object o = this.getSqlMapClientTemplate().insert("muser.insertUser",user);
		
		if(o!=null){
			return	Integer.valueOf(o.toString());
		}
		return null;
	}
	
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public boolean updateUser(User user) throws Exception{
		
		
		return this.getSqlMapClientTemplate().update("muser.updateUser",user)>0;
	}
	
	
	/**
	 * 根据ID 查询user对象
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public User getUserById(Integer id) throws Exception{
		
		
		return (User) this.getSqlMapClientTemplate().queryForObject("muser.getUserById",id);
	}
	
	
	/**
	 * 根据ID 删除用户
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean delUserById(Integer id) throws Exception{
		
		return this.getSqlMapClientTemplate().delete("muser.delUserById",id)>0;
	}
	
	/**
	 * 修改用户密码
	 * @param userId
	 * @param pwd
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserPwd(Integer userId,String pwd) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("pwd", pwd);
		return this.getSqlMapClientTemplate().update("muser.updateUserPwd",map)>0;
	}
	
	/**
	 * 登录次数
	 * @param userId
	 * @param pwd
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserCount(User user) throws Exception{
		return this.getSqlMapClientTemplate().update("muser.updateUserCount",user)>0;
	}
}
